﻿using System.Collections.Generic;
using Dou.UI;
using UnityEngine;

namespace Simple
{
    public class Test_SimpleList2 : MonoBehaviour
    {
        public List<Test_ListItemData2> datas = new List<Test_ListItemData2>();
        
        public UIDataDuplicator duplicator;

        private void Awake()
        {
            // 填充数据
            for (int i = 0; i < 10; i++)
            {
                var item = new Test_ListItemData2();
                item.text = (i + 1).ToString();
                for (int j = 0; j < 10; j++)
                {
                    var reward = new Test_ListItemRewardData2();
                    reward.pic = j % 2 == 0 ? "tex_lt_icon_biaoqing_01" : "tex_lt_icon_biaoqing_02";
                    reward.hasEffect = j % 2 == 0;
                    item.rewardList.Add(reward);
                }
                datas.Add(item);
            }
            
            duplicator.onItemInit += DuplicatorOnItemInit;
            duplicator.onItemUpdate += DuplicatorOnItemUpdate;
            duplicator.onItemDisable += DuplicatorOnItemDisable;
            duplicator.dataProvider = new ListCollection<Test_ListItemData2>(datas);
        }

        private void DuplicatorOnItemInit(GameObject go, object d, int index)
        {
            // 这里添加事件监听
            var dup = go.transform.Find("RewardList").GetComponent<UIDataDuplicator>();
            
            dup.onItemInit += RewardOnItemInit;
            dup.onItemUpdate += RewardOnItemUpdate;
            dup.onItemDisable += RewardOnItemDisable;
        }

        private void DuplicatorOnItemUpdate(GameObject go, object d, int index)
        {
            // 为了方便UI节点这里直接用 Find 来查找了
            var text = go.transform.Find("Lab").GetComponent<DTextMeshProUGUI>();
            var dup = go.transform.Find("RewardList").GetComponent<UIDataDuplicator>();
            
            var data = (Test_ListItemData2) d;
            
            text.text = data.text;
            dup.dataProvider = new ListCollection<Test_ListItemRewardData2>(data.rewardList);
        }

        private void DuplicatorOnItemDisable(GameObject go, int index)
        {
            // 这里移除事件监听
            var dup = go.transform.Find("RewardList").GetComponent<UIDataDuplicator>();
            
            dup.onItemInit -= RewardOnItemInit;
            dup.onItemUpdate -= RewardOnItemUpdate;
            dup.onItemDisable -= RewardOnItemDisable;
        }

        private void RewardOnItemInit(GameObject go, object d, int index)
        {
        }

        private void RewardOnItemUpdate(GameObject go, object d, int index)
        {
            // 为了方便UI节点这里直接用 Find 来查找了
            var image = go.transform.Find("Image").GetComponent<DImage>();
            var eff = go.transform.Find("Eff").gameObject;
            
            var data = (Test_ListItemRewardData2) d;
            
            // image.spriteName = data.pic;
            eff.SetActive(data.hasEffect);
        }

        private void RewardOnItemDisable(GameObject go, int index)
        {
        }
    }
    
    public class Test_ListItemData2
    {
        public string text;
        public List<Test_ListItemRewardData2> rewardList = new List<Test_ListItemRewardData2>();
    }

    public class Test_ListItemRewardData2
    {
        public string pic;
        public bool hasEffect;
    }
}
